盒子
盒子

leetcode 101. Symmetric Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL) {
return true;
}
else {
return isSame(root->left, root->right);
}
}
private:
bool isSame(TreeNode* left_tree, TreeNode* right_tree) {
if(left_tree == NULL && right_tree == NULL) {
return true;
}
else if(left_tree == NULL || right_tree == NULL || left_tree->val != right_tree->val) {
return false;
}
else {
return isSame(left_tree->left, right_tree->right) && isSame(left_tree->right, right_tree->left);
}
}
};